package com.base.key.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.key.domain.vo.TKeyReserveVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.key.domain.bo.TKeyReserveBo;
import com.base.key.domain.TKeyReserve;
import com.base.key.mapper.TKeyReserveMapper;
import com.base.key.service.ITKeyReserveService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 密钥备用表Service业务层处理
 *
 * @author base
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class TKeyReserveServiceImpl implements ITKeyReserveService {

    private final TKeyReserveMapper baseMapper;

    /**
     * 查询密钥备用表
     */
    @Override
    public TKeyReserveVo queryById(String keyCode){
        return baseMapper.selectVoById(keyCode);
    }

    /**
     * 查询密钥备用表列表
     */
    @Override
    public TableDataInfo<TKeyReserveVo> queryPageList(TKeyReserveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TKeyReserve> lqw = buildQueryWrapper(bo);
        Page<TKeyReserveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询密钥备用表列表
     */
    @Override
    public List<TKeyReserveVo> queryList(TKeyReserveBo bo) {
        LambdaQueryWrapper<TKeyReserve> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TKeyReserve> buildQueryWrapper(TKeyReserveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TKeyReserve> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getKeyType()), TKeyReserve::getKeyType, bo.getKeyType());
        lqw.eq(bo.getKeyLen() != null, TKeyReserve::getKeyLen, bo.getKeyLen());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TKeyReserve::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyValue()), TKeyReserve::getKeyValue, bo.getKeyValue());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckCode()), TKeyReserve::getCheckCode, bo.getCheckCode());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateStyle()), TKeyReserve::getCreateStyle, bo.getCreateStyle());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TKeyReserve::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增密钥备用表
     */
    @Override
    public Boolean insertByBo(TKeyReserveBo bo) {
        TKeyReserve add = BeanUtil.toBean(bo, TKeyReserve.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setKeyCode(add.getKeyCode());
        }
        return flag;
    }

    /**
     * 修改密钥备用表
     */
    @Override
    public Boolean updateByBo(TKeyReserveBo bo) {
        TKeyReserve update = BeanUtil.toBean(bo, TKeyReserve.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TKeyReserve entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除密钥备用表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
